package com.jianyun.jyzs.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.ThisApp;
import com.jianyun.jyzs.activity.AnnouncementActivity;
import com.jianyun.jyzs.activity.CrmActivity;
import com.jianyun.jyzs.activity.EngineeringDynamicActivity;
import com.jianyun.jyzs.activity.FlowExamineActivity;
import com.jianyun.jyzs.activity.IntegratedServiceActivity;
import com.jianyun.jyzs.activity.KaiPiaoActivity;
import com.jianyun.jyzs.activity.SigninActivity;
import com.jianyun.jyzs.activity.WarningActivity;
import com.jianyun.jyzs.activity.newsign.NewSign2Activity;
import com.jianyun.jyzs.activity.newsign.NewSignActivity;
import com.jianyun.jyzs.adapter.OAAreaAdapter;
import com.jianyun.jyzs.adapter.OATopAdapter;
import com.jianyun.jyzs.base.MVPBaseFragment;
import com.jianyun.jyzs.dao.ModelDao;
import com.jianyun.jyzs.db.EventCount;
import com.jianyun.jyzs.presenter.OAFragPresenter;
import com.jianyun.jyzs.qrcode.IntentIntegrator;
import com.jianyun.jyzs.qrcode.barcodescanner.CaptureActivity;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.utils.SPUtils;
import com.jianyun.jyzs.view.iview.IOAFragView;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.rong.eventbus.EventBus;

/**
 * Created by yangshijun on 2018/5/25.
 */

public class OAFragment extends MVPBaseFragment<IOAFragView, OAFragPresenter> implements IOAFragView {


    @BindView(R.id.GridViewHead)
    android.widget.GridView GridViewHead;
    @BindView(R.id.GridView)
    android.widget.GridView GridView;

    Unbinder unbinder;

    private View rootView;
    private String userId;
    private String enterpriseCode;
    private ModelDao dao;
    private OAAreaAdapter areaAdapter;
    private OATopAdapter topAdapter;
    private char[] charArray;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null != rootView) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (null != parent) {
                parent.removeView(rootView);
            }
        } else {
            rootView = inflater.inflate(R.layout.fragment_app_module, null);
            String enableLogin = LoginCache.getInstance().getEnableLogin();
            if (!TextUtils.isEmpty(enableLogin)) {
                charArray = enableLogin.toCharArray();
            }
            //initView();// 控件初始化
        }
        EventBus.getDefault().register(this);
        unbinder = ButterKnife.bind(this, rootView);

        return rootView;
    }

    @Override
    protected void initView() {
        enterpriseCode = LoginCache.getInstance().getLoginCache().getEnterpriseCode();

        userId = LoginCache.getInstance().getUserInfo().getUserId();
        dao = new ModelDao(getActivity());
        areaAdapter = new OAAreaAdapter(getActivity(), null);
        GridView.setAdapter(areaAdapter);
        topAdapter = new OATopAdapter(getActivity(), null);
        GridViewHead.setAdapter(topAdapter);
        GridViewHead.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i) {
                    case 0:
                        //综合业务
                        startActivity(new Intent(getActivity(), IntegratedServiceActivity.class));
                        break;
                    case 1:
                        //流程审批
                        startActivity(new Intent(getActivity(), FlowExamineActivity.class));
                        break;
                    case 2:
                        //扫一扫
                        startActivityForResult(new Intent(getActivity(), CaptureActivity.class), IntentIntegrator.REQUEST_CODE);
                        break;
                    case 3:
                        String version = LoginCache.getInstance().getSignVersion().getVersion();
//                        if ("1".equals(version)) {
//                            //签到
//                            startActivity(new Intent(getActivity(), NewSignActivity.class));
//                        } else if ("2".equals(version)) {
//                            startActivity(new Intent(getActivity(), NewSign2Activity.class));
//                        } else if ("3".equals(version)) {
//                            startActivity(new Intent(getActivity(), NewSignActivity.class));
//                        } else if ("4".equals(version)) {
//                            startActivity(new Intent(getActivity(), NewSign2Activity.class));
//                        }else {
                            startActivity(new Intent(getActivity(), SigninActivity.class));
//                        }
                        break;
                }
            }
        });
        GridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i) {
                    case 0:
//                        try {
//                            char c = charArray[6];
//                            if (charArray != null) {
//                                if ("1".equals(charArray[6] + "")) {
//                                    //通知公告
//                                    startActivity(new Intent(getActivity(), AnnouncementActivity.class));
//                                }
//                            } else {
                        //通知公告
                        startActivity(new Intent(getActivity(), AnnouncementActivity.class));
//                            }
//                        } catch (Exception e) {
//                            ToastUtil.showToast(getActivity(), "没有权限");
//                        }


                        break;
                    case 1:
                        //开票
                        Intent intent = new Intent(getActivity(), KaiPiaoActivity.class);
                        intent.putExtra("URL", LoginCache.getInstance().getLoginCache().getErpRootUrl() + "Phone/InvoiceInfo.aspx");
                        startActivity(intent);
                        break;
                    case 2:
//                        try {
//                            char c = charArray[7];
//                            if (charArray != null) {
//                                if ("1".equals(charArray[7] + "")) {
//                                    //预警消息
//                                    startActivity(new Intent(getActivity(), WarningActivity.class));
//                                }
//                            } else {
                        //预警消息
                        startActivity(new Intent(getActivity(), WarningActivity.class));
//                            }
//                        } catch (Exception e) {
//                            ToastUtil.showToast(getActivity(), "没有权限");
//                        }


                        break;
                    case 3:
//                        try {
//                            char c = charArray[16];
//                            if (charArray != null) {
//                                if ("1".equals(charArray[16] + "")) {
//                                    //CRM
//                                    startActivity(new Intent(getActivity(), CrmActivity.class));
//                                }
//                            } else {
                        //CRM
                        startActivity(new Intent(getActivity(), CrmActivity.class));
//                            }
//                        } catch (Exception e) {
//                            ToastUtil.showToast(getActivity(), "没有权限");
//                        }


                        break;
                    case 4:
//                        try {
//                            char a = charArray[26];
//                            if (charArray != null) {
//                                if ("1".equals(charArray[26] + "")) {
//                                    //工程动态
//                                    startActivity(new Intent(getActivity(), EngineeringDynamicActivity.class));
//                                }
//                            } else {
                        //工程动态
                        startActivity(new Intent(getActivity(), EngineeringDynamicActivity.class));
//                            }
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                            ToastUtil.showToast(getActivity(), "没有权限");
//                            startActivity(new Intent(getActivity(), EngineeringDynamicActivity.class));
//                        }


//                        if (charArray != null) {
//                            if ("1".equals(charArray[0] + "")) {
//                                //工单
//                                startActivity(new Intent(getActivity(), WorkOrderActivity.class));
//                            } else {
//                                ToastUtil.showToast(getActivity(), "没有权限");
//                            }
//                        } else {
//                            //工单
//                            startActivity(new Intent(getActivity(), WorkOrderActivity.class));
//                        }

                        break;
                    case 5:
//                        if (charArray != null) {
//                            if ("1".equals(charArray[1] + "")) {
//                                //质量检查
//                                startActivity(new Intent(getActivity(), QualityInspectionActivity.class));
//                            } else {
//                                ToastUtil.showToast(getActivity(), "没有权限");
//                            }
//                        } else {
//                            //质量检查
//                            startActivity(new Intent(getActivity(), QualityInspectionActivity.class));
//                        }

                        break;
                    case 6:
//                        if (charArray != null) {
//                            if ("1".equals(charArray[2] + "")) {
//                                //安全检查
//                                startActivity(new Intent(getActivity(), SecurityCheckActivity.class));
//                            } else {
//                                ToastUtil.showToast(getActivity(), "没有权限");
//                            }
//                        } else {
//                            //安全检查
//                            startActivity(new Intent(getActivity(), SecurityCheckActivity.class));
//                        }

                        break;
                    case 7:
//                        if (charArray != null) {
//                            if ("1".equals(charArray[26] + "")) {
//                                //工程动态
//                                startActivity(new Intent(getActivity(), EngineeringDynamicActivity.class));
//                            } else {
//                                ToastUtil.showToast(getActivity(), "没有权限");
//                            }
//                        } else {
//                            //工程动态
//                            startActivity(new Intent(getActivity(), EngineeringDynamicActivity.class));
//                        }

                        break;
                }
            }
        });
    }

    //出来evenbus收到的消息
    public void onEvent(final EventCount event) {
        ThisApp.applicationHandler.post(new Runnable() {
            @Override
            public void run() {
                int count = event.getCount();
                topAdapter.setSpCunt(count);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        // List<ItemModel> modelList = dao.getModelList(userId, enterpriseCode, 1);
        //Log.i("test", "数据：" + modelList.toString());
        areaAdapter.setList(dao.getModelList(userId, enterpriseCode, 1));
        topAdapter.setList(dao.getModelList(userId, enterpriseCode, 0));
        //设置 应用红色角标
        int count = (int) SPUtils.get(getActivity(), userId, 0);
        topAdapter.setSpCunt(count);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int initContentLayout() {
        return 0;
    }

    @Override
    public OAFragPresenter createPresenter() {
        return new OAFragPresenter();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == IntentIntegrator.REQUEST_CODE) {
            if (data!=null){
                String url = data.getStringExtra("url");
                if (url != null && !TextUtils.isEmpty(url)) {
                    Uri uri = Uri.parse(url);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                }
            }
        }
    }
}
